package com.pimwi.service
{
  import com.pimwi.dp.ContactDP;
  import com.pimwi.dp.GroupDP;
  import com.pimwi.model.ModelLocator;
  import com.pimwi.vo.Contact;
  import com.pimwi.vo.request.DeleteContactRequest;
  import com.pimwi.vo.request.FindContactsByUserRequest;
  import com.pimwi.vo.request.SaveContactRequest;
  
  import mx.collections.ArrayCollection;
  import mx.controls.Alert;
  import mx.rpc.events.ResultEvent;
  
  public class ContactService extends BaseService
  {
    public function ContactService()
    {
      super();
    }

    /***************************/
    /** FIND CONTACTS BY USER **/
    /***************************/

    public function findContactsByUser(request:FindContactsByUserRequest):void
    {
      _service.findContactsByUser.addEventListener(ResultEvent.RESULT, findContactsByUserResultHandler);
      _service.findContactsByUser(request);
    }

    private function findContactsByUserResultHandler(event:ResultEvent):void
    {
      _service.findContactsByUser.removeEventListener(ResultEvent.RESULT, findContactsByUserResultHandler);

      var response:Object = event.result;
      if (response.errorMessage != "")
      {
        Alert.show(response.errorMessage, "Erreur");
      }
      else
      {
        updateUserContacts(response.userContacts);
      }
    }

    /******************/
    /** SAVE CONTACT **/
    /******************/

    public function saveContact(request:SaveContactRequest):void
    {
      _service.saveContact.addEventListener(ResultEvent.RESULT, saveContactResultHandler);
      _service.saveContact(request);
    }

    private function saveContactResultHandler(event:ResultEvent):void
    {
      _service.saveContact.removeEventListener(ResultEvent.RESULT, saveContactResultHandler);

      var response:Object = event.result;
      if (response.errorMessage != "")
      {
        Alert.show(response.errorMessage, "Erreur");
      }
      else
      {
        updateUserContacts(response.userContacts);
      }
    }

    /********************/
    /** DELETE CONTACT **/
    /********************/

    public function deleteContact(request:DeleteContactRequest):void
    {
      _service.deleteContact.addEventListener(ResultEvent.RESULT, deleteContactResultHandler);
      _service.deleteContact(request);
    }

    private function deleteContactResultHandler(event:ResultEvent):void
    {
      _service.deleteContact.removeEventListener(ResultEvent.RESULT, deleteContactResultHandler);

      var response:Object = event.result;
      if (response.errorMessage != "")
      {
        Alert.show(response.errorMessage, "Erreur");
      }
      else
      {
        updateUserContacts(response.userContacts);
        ModelLocator.getInstance().currentContactDP.contact = null;
      }
    }

    /*************/
    /** PRIVATE **/
    /*************/

    private function updateUserContacts(retrievedContacts:ArrayCollection):void // list of Contact
    {
      var locator:ModelLocator = ModelLocator.getInstance();
      var proxy:Object = null;
      var userContacts:ArrayCollection = new ArrayCollection(); // list of ContactDP
      var contact:Contact = null;
      for each (proxy in retrievedContacts)
      {
        contact = new Contact(proxy.id, proxy.userId, proxy.firstName, proxy.lastName, proxy.address, proxy.city, proxy.email, proxy.phone, proxy.groupId);
        var contactDP:ContactDP = new ContactDP(contact);
        userContacts.addItem(contactDP);
      }
      locator.userContacts = userContacts;

      var filteredContacts:ArrayCollection = new ArrayCollection();
      var filteredGroup:GroupDP = locator.taskBarDP.filteredGroup;
      for each (var _contact:ContactDP in userContacts)
      {
        if (filteredGroup.id == -1 || _contact.group.id == filteredGroup.id)
        {
          filteredContacts.addItem(_contact);
        }
      }
      locator.filteredContacts = filteredContacts;
    }

  }
}